const { isEmpty } = require('lodash');

/**
 * 请求中间件
*/
module.exports = () => {
  return async function requestHandler(ctx, next) {
    await next();

    const { request: { method, body } } = ctx;

    // post 请求参数不合法时抛出错误
    if ((method === 'POST') && isEmpty(body)) {
      ctx.status = 400;
      ctx.body = {
        message: '参数错误',
      };
    }
  };
};
